Okay, so in today's lesson we are going to have a look at how to script tools.
Now hopefully you should already know a bit about tools.
You should know what they are and how to set them up.
Okay, if you need a bit of a reminder, go watch the previous lecture on tools.
Um, in that lecture we kind of focused on actually creating them rather than coding them.
Today we're just focusing on coding them.
Okay, now to save a bit of time, I've already got a tool just from a toolbox into start into a starter
pack.
And I've just gotten rid of any scripts in there and we're going to have a go at scripting it ourself.
Okay.
So just so just to show you what it looks like, I'm going to put it into the workspace.
And you can see the tool just here.
It's just a little um torch or flashlight here.
Okay.
Um I'm going to have a go at coding this.
Okay.
So, um, let's add a local script inside of the flashlight.
Okay.
Why a local script?
Well, tools are going to be picked up by one player, okay?
And anything that happens with tools is usually done locally, okay.
Because the player is going to be picking up, picking it up, and the player is going to be equipping
it and activating it.
Okay.
So while you might want to do some things on the server, we would use a local script mostly for tools.
Okay.
So in here let's reference the tool.
So let's say local um tool is equal to script dot parent in this case okay.
Just like how with GIS you can't reference using GameStar GI, you can't really reference using game
dot starter pack either because tools let's play the game and actually see where they are.
So the inside plays I've got my player here, I've got my backpack and I've got my light here.
So tools exist in the backpack of your player when they are not equipped.
However, if I press one to equip this, it goes, okay, I've teleported over here, which will explain
why in a minute, but it disappears on the backpack.
That's because it then goes inside of the player's character.
You can see there the torch or the flashlight here.
Okay, so um, that's why I had to reference using script dot parent, because it moves, um, to inside
the player and also into the character when it's been equipped as well.
Okay.
So there's a lot of moving around with doing.
Only because it's anchored.
Yes.
On anchor app, please.
There we go.
Let's just double check to make sure it works before we do start coding it.
So one again.
And now it works completely fine.
Just like that as well.
Cool.
So let's actually code it now.
Okay.
So in the local script coding tools, just like anything else, it's you already know how to do it.
There's just a couple things you need to be aware of in order to properly code them.
Okay.
So in this case there are three events that you need to know.
Okay.
Once you know those three events, you can code any tools you like using the previous knowledge you
have.
Okay.
The first one is called equipped.
And you can get these events.
Dot and then naming the event.
So the event here is called equipped.
Okay.
And this will funnily enough fire when the tool is equipped.
Okay.
Colon connect function just like that there.
And we can just print here.
Equipped okay.
So that they will run when the tool is equipped.
You can also do the opposite and run an event or run a function when it's unequipped which is tool dot
unequipped okay.
Tiny crypt and the final one, which is the most useful one?
To be honest, is too activated.
Now what does activated mean?
Well, activated means that the player is trying to use the tool.
Okay.
And this happens when two things occur.
One, the player has the tool equipped and two the player left clicks somewhere on their screen.
This means if you want your tool to work, we have to have it equipped and left click somewhere on the
screen and it picks it up as activating the tool.
So what?
That's what this event does here.
Okay come on.
Connect function.
Print activated.
Okay.
So the majority of tools are used by just equipping and left clicking somewhere on the screen.
Other ones might be used different ways.
There be different ways to code those.
But typically for the more basic tools you would be looking at activated there.
Okay, so let's just quickly play the game and make sure this all works.
Okay.
So I have my tool here.
I'm going to press one.
And it's equipped I'm going to left click and it press activated.
And I'm going to unequip.
And it prints on equipped.
Perfect.
So let's just have a look as we have a go encoding this.
So let's put this back into the workspace just to see.
So the flashlight or the torch is going to be turned on and off okay.
It has a spotlight in here which we can just change enable to be on and off.
And that's all we're really going to need to do for this okay.
So in our local script um, when the tool is activated so we don't, we don't really want to be using
these ones just yet okay.
We're focusing on this one here.
So when the tool is activated um, well we want to be able to turn on the torch, but we also want to
be able to turn it off.
So kind of like how we're doing is we check if the frame is visible first and then if it is visible,
we're making it invisible.
And if it's invisible, we make it visible.
So we do the opposite.
And that's what we're doing here.
If if a torch is currently on, we want to turn it off and it's currently off, we want to turn it on
okay.
So we use an if statement for that.
So we would check if tool dot handle dot spotlight.
That's how it's referenced here.
But again for your if you're making a torch or if you're making something else, it would differ based
on where everything is laid out in your explorer okay dot enabled.
So if a tool is currently enabled then sorry.
If the spotlight is currently enabled then disable it.
So two dot handle dot spotlight dot enabled is equal to false.
Else make it true.
I probably could have made a variable for this to be honest, but that's fine.
And that's all the code there.
So if it's currently enabled then make enabled off.
If enabled is currently off turn it to be true.
So it's just doing the opposite okay.
So let's try this out now then let's load into the game.
Oh I've forgotten that we need it to go into starter pack in order for this to work okay.
Otherwise it won't be in the player's inventory to start off with.
Or wear backpack.
It's best one.
It's for some reason anchored it again.
But you can see here when we left click it now works okay.
And I left click again and it disappeared.
Obviously it's difficult for you to tell when I'm left clicking, but I can promise you I am left clicking
now.
Okay?
So just clicks on and off quite nicely like that okay.
And obviously it's decided to anchor it again.
So let's on anchor that and we just try out one more time so it works.
Let me go.
Flashlight on and we can equip it.
Turn on and off like that.
Okay.
Cool.
So tools really aren't that difficult to code at all.
Okay, you just need to know the correct events that you need to use.
Okay.
Um, and also you need to set them up correctly as how we explained in the last lesson on tools.
Okay.
Um, but yeah, that's pretty much it.
Okay.
We didn't go any look at anything to do with the equipped and unequipped events.
Um, usually for those in the equipped and unequipped, you would enable any guys that need to be on,
um, or for example, um, you might play a sound as well to show that actually you've equipped the
tool, which is, funnily enough, what we're going to have a look at next lesson.
Okay.
So join me in the next lesson and we're going to continue on looking at this flashlights.
But we're going to learn about how to add sounds onto this.
Okay.
So make sure you understand this and then join me in the next lesson.
We have a look at sounds.